from typing import List


class Solution:
    def gridGame(self, grid: List[List[int]]) -> int:
        n = len(grid[0])

        prefix = [0] * (n + 1)
        suffix = [0] * (n + 1)

        for i in range(1, (n + 1)):
            prefix[i] = prefix[i - 1] + grid[1][i - 1]

        for i in range(n - 1, -1, -1):
            suffix[i] = suffix[i + 1] + grid[0][i]

        suffix.insert(0, 0)
        prefix.append(0)

        ans = min(suffix[1], prefix[-2])
        for i in range(1, (n + 1)):
            ans = min(ans, max(suffix[i + 1], prefix[i - 1]))
        return ans


if __name__ == "__main__":
    print(Solution().gridGame(grid=[[2, 5, 4], [1, 5, 1]]))  # 4
    print(Solution().gridGame(grid=[[3, 3, 1], [8, 5, 2]]))  # 4
    print(Solution().gridGame(grid=[[1, 3, 1, 15], [1, 3, 3, 1]]))  # 7
